package com.stylefeng.guns.film.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.film.FilmServiceApi;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.film.dao.*;
import com.stylefeng.guns.film.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dengwubo
 */
@Component
@Service
public class DefaultFilmServiceImpl implements FilmServiceApi {
    @Autowired
    private MoocBannerTMapper bannerMapper;
    @Autowired
    private MoocFilmTMapper filmMapper;
    @Autowired
    private MoocCatDictTMapper catMapper;
    @Autowired
    private MoocYearDictTMapper yearMapper;
    @Autowired
    private MoocSourceDictTMapper sourceMapper;
    @Autowired
    private MoocFilmInfoTMapper filmInfoMapper;
    @Autowired
    private MoocActorTMapper actorMapper;


    @Override
    public List<BannerVO> getBanners() {
        List<BannerVO> result = new ArrayList<>();
        List<MoocBannerT> banners = bannerMapper.selectList(null);
        for (MoocBannerT banner : banners) {
            BannerVO bannerVO = new BannerVO();
            bannerVO.setBannerUrl(banner.getBannerUrl());
            bannerVO.setBannerId(banner.getUuid() + "");
            bannerVO.setBannerAddress(banner.getBannerAddress());
            result.add(bannerVO);
        }

        return result;
    }

    private List<FilmInfo> getFilmInfos(List<MoocFilmT> moocFilms) {
        List<FilmInfo> filmInfos = new ArrayList<>();
        moocFilms.forEach(moocFilm -> {
            FilmInfo filmInfo = new FilmInfo();
            filmInfo.setShowTime(DateUtil.formatTime(moocFilm.getFilmTime()));
            filmInfo.setScore(moocFilm.getFilmScore());
            filmInfo.setImgAddress(moocFilm.getImgAddress());
            filmInfo.setFilmType(moocFilm.getFilmType() + "");
            filmInfo.setFilmScore(moocFilm.getFilmScore());
            filmInfo.setFilmName(moocFilm.getFilmName());
            filmInfo.setFilmId(moocFilm.getUuid() + "");
            filmInfo.setExceptNum(moocFilm.getFilmPresalenum());
            filmInfo.setBoxNum(moocFilm.getFilmBoxOffice());
            filmInfos.add(filmInfo);
        });
        return filmInfos;
    }

    @Override
    public FilmVO getHotFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 热映影片的限制条件
        EntityWrapper<MoocFilmT> wrapper = new EntityWrapper<>();
        wrapper.eq("film_status", "1");
        // 判断是否是首页需要的内容
        if (isLimit) {
            // 如果是,限制条数 限制内容为热映影片
            Page<MoocFilmT> page = new Page<>(1, nums);
            List<MoocFilmT> moocFilms = filmMapper.selectPage(page, wrapper);
            filmInfos = getFilmInfos(moocFilms);
            filmVO.setFilmInfos(filmInfos);
            filmVO.setFilmNum(filmInfos.size());

        } else {
            // 如果不是,则是列表页,同样需要限制内同为热映影片
            Page<MoocFilmT> page = null;
            switch (sortId) {
                case 1:
                    page = new Page<>(nowPage, nums, "film_box_office");
                    break;
                case 2:
                    page = new Page<>(nowPage, nums, "film_time");
                    break;
                case 3:
                    page = new Page<>(nowPage, nums, "film_score");
                    break;
                default:
                    page = new Page<>(nowPage, nums, "film_box_office");
                    break;

            }

            if (sourceId != 99) {
                wrapper.eq("film_source", sourceId);
            }
            if (yearId != 99) {
                wrapper.eq("film_date", yearId);
            }
            if (catId != 99) {
                String catStr = "%#" + catId + "#%";
                wrapper.like("film_cats", catStr);
            }


            List<MoocFilmT> moocFilms = filmMapper.selectPage(page, wrapper);
            Integer totalPages = (int) page.getPages();
            filmInfos = getFilmInfos(moocFilms);
            filmVO.setFilmInfos(filmInfos);
            filmVO.setFilmNum(filmInfos.size());
            filmVO.setTotalPage(totalPages);
            filmVO.setNowPage(nowPage);

        }

        return filmVO;
    }

    @Override
    public FilmVO getSoonFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 即将上映影片的限制条件
        EntityWrapper<MoocFilmT> wrapper = new EntityWrapper<>();
        wrapper.eq("film_status", "2");
        // 判断是否是首页需要的内容
        if (isLimit) {
            // 如果是,限制条数 限制内容为即将上映影片
            Page<MoocFilmT> page = new Page<>(1, nums);
            List<MoocFilmT> moocFilms = filmMapper.selectPage(page, wrapper);
            filmInfos = getFilmInfos(moocFilms);
            filmVO.setFilmInfos(filmInfos);
            filmVO.setFilmNum(filmInfos.size());

        } else {
            // 如果不是,则是列表页,同样需要限制内同为即将上映影片
            Page<MoocFilmT> page = null;
            switch (sortId) {
                case 1:
                    page = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;
                case 2:
                    page = new Page<>(nowPage, nums, "film_time");
                    break;
                case 3:
                    page = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;
                default:
                    page = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;

            }
            if (sourceId != 99) {
                wrapper.eq("film_source", sourceId);
            }
            if (yearId != 99) {
                wrapper.eq("film_date", yearId);
            }
            if (catId != 99) {
                String catStr = "%#" + catId + "#%";
                wrapper.like("film_cats", catStr);
            }


            List<MoocFilmT> moocFilms = filmMapper.selectPage(page, wrapper);
            Integer totalPages = (int) page.getPages();
            filmInfos = getFilmInfos(moocFilms);
            filmVO.setFilmInfos(filmInfos);
            filmVO.setFilmNum(filmInfos.size());
            filmVO.setTotalPage(totalPages);
            filmVO.setNowPage(nowPage);
        }

        return filmVO;
    }

    @Override
    public FilmVO getClassicFilms(int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 经典影片的限制条件
        EntityWrapper<MoocFilmT> wrapper = new EntityWrapper<>();
        wrapper.eq("film_status", "3");
        Page<MoocFilmT> page = null;
        switch (sortId) {
            case 1:
                page = new Page<>(nowPage, nums, "film_box_office");
                break;
            case 2:
                page = new Page<>(nowPage, nums, "film_time");
                break;
            case 3:
                page = new Page<>(nowPage, nums, "film_score");
                break;
            default:
                page = new Page<>(nowPage, nums, "film_box_office");
                break;

        }

        if (sourceId != 99) {
            wrapper.eq("film_source", sourceId);
        }
        if (yearId != 99) {
            wrapper.eq("film_date", yearId);
        }
        if (catId != 99) {
            String catStr = "%#" + catId + "#%";
            wrapper.like("film_cats", catStr);
        }


        List<MoocFilmT> moocFilms = filmMapper.selectPage(page, wrapper);
        Integer totalPages = (int) page.getPages();
        filmInfos = getFilmInfos(moocFilms);
        filmVO.setFilmInfos(filmInfos);
        filmVO.setFilmNum(filmInfos.size());
        filmVO.setTotalPage(totalPages);
        filmVO.setNowPage(nowPage);
        return filmVO;
    }

    @Override
    public List<FilmInfo> getBoxRanking() {
        //  条件 -> 正在上映的,票房前10名
        EntityWrapper<MoocFilmT> wrapper = new EntityWrapper<>();
        wrapper.eq("film_status", "1");
        Page<MoocFilmT> page = new Page<>(1, 10, "film_box_office");
        List<MoocFilmT> moocFilms = filmMapper.selectPage(page, wrapper);
        List<FilmInfo> filmInfos = getFilmInfos(moocFilms);
        return filmInfos;
    }

    @Override
    public List<FilmInfo> getExpectRanking() {
        //  条件 -> 即将上映的,预售前10名
        EntityWrapper<MoocFilmT> wrapper = new EntityWrapper<>();
        wrapper.eq("film_status", "2");
        Page<MoocFilmT> page = new Page<>(1, 10, "film_preSaleNum");
        List<MoocFilmT> moocFilms = filmMapper.selectPage(page, wrapper);
        List<FilmInfo> filmInfos = getFilmInfos(moocFilms);
        return filmInfos;
    }

    @Override
    public List<FilmInfo> getTop() {
        EntityWrapper<MoocFilmT> wrapper = new EntityWrapper<>();
        wrapper.eq("film_status", "1");
        Page<MoocFilmT> page = new Page<>(1, 100, "film_score");
        List<MoocFilmT> moocFilms = filmMapper.selectPage(page, wrapper);
        List<FilmInfo> filmInfos = getFilmInfos(moocFilms);
        return filmInfos;
    }

    @Override
    public List<CatVO> getCats() {
        List<CatVO> result = new ArrayList<>();

        List<MoocCatDictT> moocCats = catMapper.selectList(null);
        for (MoocCatDictT moocCat : moocCats) {
            CatVO catVO = new CatVO();
            catVO.setCatId(moocCat.getUuid() + "");
            catVO.setCatName(moocCat.getShowName());
            result.add(catVO);
        }

        return result;
    }

    @Override
    public List<SourceVO> getSources() {
        List<SourceVO> result = new ArrayList<>();
        List<MoocSourceDictT> moocSources = sourceMapper.selectList(null);
        for (MoocSourceDictT moocSource : moocSources) {
            SourceVO sourceVO = new SourceVO();
            sourceVO.setSourceName(moocSource.getShowName());
            sourceVO.setSourceId(moocSource.getUuid() + "");
            result.add(sourceVO);
        }
        return result;
    }

    @Override
    public List<YearVO> getYears() {
        List<YearVO> result = new ArrayList<>();
        List<MoocYearDictT> moocYears = yearMapper.selectList(null);
        for (MoocYearDictT moocYear : moocYears) {
            YearVO yearVO = new YearVO();
            yearVO.setYearName(moocYear.getShowName());
            yearVO.setYearId(moocYear.getUuid() + "");
            result.add(yearVO);
        }
        return result;
    }

    @Override
    public FilmDetailVO getFilmDetail(int searchType, String searchParam) {

        FilmDetailVO filmDetailVO;

        if (searchType == 1) {
            filmDetailVO = filmMapper.getFilmDetailByName(searchParam);
        } else {
            filmDetailVO = filmMapper.getFilmDetailById(searchParam);
        }

        return filmDetailVO;
    }

    private MoocFilmInfoT getFilmInfo(String filmId) {
        MoocFilmInfoT moocFilmInfoT = new MoocFilmInfoT();
        moocFilmInfoT.setFilmId(filmId);
        moocFilmInfoT = filmInfoMapper.selectOne(moocFilmInfoT);
        return moocFilmInfoT;
    }

    @Override
    public FilmDescVO getFilmDesc(String filmId) {
        MoocFilmInfoT filmInfo = getFilmInfo(filmId);
        FilmDescVO filmDescVO = new FilmDescVO();
        filmDescVO.setBiography(filmInfo.getBiography());
        filmDescVO.setFilmId(filmId);
        return filmDescVO;
    }

    @Override
    public ImgVO getImgs(String filmId) {
        MoocFilmInfoT filmInfo = getFilmInfo(filmId);
        String filmImgStr = filmInfo.getFilmImgs();
        String[] filmImgs = filmImgStr.split(",");
        ImgVO imgVO = new ImgVO();
        imgVO.setMainImg(filmImgs[0]);
        imgVO.setImg01(filmImgs[1]);
        imgVO.setImg02(filmImgs[2]);
        imgVO.setImg03(filmImgs[3]);
        imgVO.setImg04(filmImgs[4]);
        return imgVO;
    }

    @Override
    public ActorVO getDirectorInfo(String filmId) {
        MoocFilmInfoT filmInfo = getFilmInfo(filmId);
        // 获取导演编号
        Integer directorId = filmInfo.getDirectorId();
        MoocActorT moocActorT = actorMapper.selectById(directorId);
        ActorVO actorVO = new ActorVO();
        actorVO.setImgAddress(moocActorT.getActorImg());
        actorVO.setDirectorName(moocActorT.getActorName());
        return actorVO;
    }

    @Override
    public List<ActorVO> getActors(String filmId) {
        List<ActorVO> actors = actorMapper.getActors(filmId);
        return actors;
    }
}
